package com.hmdp.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Component
public class RedisID {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    private static final Long BEGIN=1735689600L;
    private static final Long BITS=32L;
    public Long ID(String key){
        //生成时间戳
        LocalDateTime now = LocalDateTime.now();
        //生成秒的值
        long l = now.toEpochSecond(ZoneOffset.UTC);
        long time = l - BEGIN;
        //生成序列号
        String format = now.format(DateTimeFormatter.ofPattern("yyyy:MM:dd"));
        Long increment = stringRedisTemplate.opsForValue().increment("inc:" + key + ":" + format);
        //拼接,时间戳前移32位
        return time<<BITS | increment;
    }
}






